package com.lingtianyu.tech.erbaoshen.core.util;

import com.lingtianyu.tech.erbaoshen.core.config.ErBaoShenRpcApplicationContextAware;
import org.springframework.core.env.Environment;

public class KeyUtil {
    public static String getKey(String className, String group, String version) {
        group = group != null ? group : "";
        version = version != null ? version : "";
        return "erBaoShenProvider:" + className + ":" + group + ":" + version;
    }

    public static String getProviderKey(String className, String group, String version) {
        Environment environment = ErBaoShenRpcApplicationContextAware.getApplicationContext().getEnvironment();
        group = group != null ? group : "";
        if(group.isEmpty()) {
            String property = environment.getProperty(ErBaoShenFinals.PROPERTIES_PROVIDER_GROUP);
            if(property != null && !property.isEmpty()) {
                group = property;
            }
        }

        version = version != null ? version : "";
        if(version.isEmpty()) {
            String property = environment.getProperty(ErBaoShenFinals.PROPERTIES_PROVIDER_VERSION);
            if(property != null && !property.isEmpty()) {
                version = property;
            }
        }
        return "erBaoShenProvider:" + className + ":" + group + ":" + version;
    }

    public static String getConsumerKey(String className, String group, String version) {
        Environment environment = ErBaoShenRpcApplicationContextAware.getApplicationContext().getEnvironment();
        group = group != null ? group : "";
        if(group.isEmpty()) {
            String property = environment.getProperty(ErBaoShenFinals.PROPERTIES_CONSUMER_GROUP);
            if(property != null && !property.isEmpty()) {
                group = property;
            }
        }

        version = version != null ? version : "";
        if(version.isEmpty()) {
            String property = environment.getProperty(ErBaoShenFinals.PROPERTIES_CONSUMER_VERSION);
            if(property != null && !property.isEmpty()) {
                version = property;
            }
        }
        return "erBaoShenProvider:" + className + ":" + group + ":" + version;
    }
}
